package weaver.formmode.customjavacode.browser;

import com.customization.assets.util.DBTools;
import com.customization.assets.util.DateTools;
import com.customization.commons.LogTool;
import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractCustomSqlConditionJavaCode;
import weaver.hrm.User;

import java.util.Map;

public class SwzcBrowserRight extends AbstractCustomSqlConditionJavaCode {
    private LogTool log = new LogTool("/log/assetlog/huangwm/SwzcBrowserRight",false);
    private static DBTools dbTools = new DBTools();
    private static DateTools dateTools = new DateTools();
    @Override
    public String generateSqlCondition(Map<String, Object> param) throws Exception {
        String sqlCondition = " ";
        Map<String, Object> extensionParam = (Map<String, Object>) param.get("extensionParam");//其他参数

        User user = (User)param.get("user");
        String userid = user.getUID()+"";
        String userDep = user.getUserDepartment()+"";
        String userSub = user.getUserSubCompany1()+"";

        if(user==null || userid.equals("")){
            sqlCondition = " 1!=1";
            log.writeLog("实物资产浏览按钮根据当前用户过滤数据>>>当前用户未获取到  不返回数据");
            return sqlCondition;
        }

        boolean isSysAdmin = dbTools.checkIsAdmin(userid, "42");//系统管理员
        boolean isBmAdmin = dbTools.checkIsAdmin(userid, "43");//部门管理员
        boolean isKsAdmin = dbTools.checkIsAdmin(userid, "44");//科室管理员
        boolean isZsAdmin = dbTools.checkIsAdmin(userid, "45");//中山海关管理员
//        boolean isZsSub = false;
//        RecordSet rs = new RecordSet();
//        String sql = "select id from HrmDepartment where subcompanyid1 in (select id from HrmSubCompany where id=5 or supsubcomid=5) and id="+userDep;
//        log.writeLog("根据当前用户部门id判断是否属于中山海关下的科室");
//        rs.execute(sql);
//        if (rs.next()){
//            log.writeLog("当前用户属于中山海关");
//            isZsSub = true;
//        }

        if (isSysAdmin || userid.equals("1")){
            sqlCondition = " 1=1";
            log.writeLog("当前用户id "+userid+" 属于系统管理员 sqlCondition="+sqlCondition);
            return sqlCondition;
        }
        if(isZsAdmin){
            sqlCondition = " dybm in (select id from HrmSubCompany where id=5 or supsubcomid=5) ";
            log.writeLog("当前用户id "+userid+" 属于中山海关管理员 sqlCondition="+sqlCondition);
            return sqlCondition;
        }

        if(isBmAdmin){
            sqlCondition = " dybm="+userSub+"";
            log.writeLog("当前用户id "+userid+" 属于部门管理员 sqlCondition="+sqlCondition);
            return sqlCondition;
        }

        if(isKsAdmin){
            sqlCondition = " syks="+userDep+"";
            log.writeLog("当前用户id "+userid+" 属于科室管理员 sqlCondition="+sqlCondition);
            return sqlCondition;
        }

        sqlCondition = " zrr="+userid+"";
        log.writeLog("当前用户id "+userid+" 为个人用户 sqlCondition="+sqlCondition);

        return sqlCondition;
    }
}
